package com.blog.util.md5;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author cgx
 * @Date 2021/8/28 19:26
 *
 * Md5工具类
 */
public class MD5Util {


    /**
     * 拼接盐值
     * @param
     * @return
     */
    public static String appendSalt(String pwd,String md5Key){
        return md5Key+pwd;
    }

    /**
     * 获取md5加密后的密码
     * @param
     * @return
     */
    public static String getMd5(String pwd,String md5Key){
        String encryption = pwd+md5Key;
        return DigestUtils.md5DigestAsHex(encryption.getBytes(StandardCharsets.UTF_8));
    }
    /**
     * 判断是否相同
     * @param pwd 传入比较的密码
     * @param password MD5加密的密码。一般是从数据库中查询
     * @param md5Key MD5盐值
     * @return true 相同  false 不相同
     */
    public static boolean equalMd5(String pwd,String password,String md5Key){
        String comparePwd = getMd5(pwd,md5Key);
        if (comparePwd.equalsIgnoreCase(password)){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        //拼接盐值
//        System.out.println(appendSalt("123456","blog"));
        //获取md5字符串
        System.out.println(getMd5("123456","blog"));
        //比较密码
//        String pwd = "123456";
//        String password = "5e15efc6bb4299bc9840a6a43f5346ce";
//        System.out.println(getMd5("123456","blog").equalsIgnoreCase(password));
    }
}
